﻿using System;
using Flagwind.Plugins;
using Flagwind.Services;

namespace Flagwind.Terminals.Plugins
{
	public class Workbench : Flagwind.Plugins.WorkbenchBase
	{
		#region 成员字段

		private TerminalCommandExecutor _executor;

		#endregion

		#region 构造方法

		public Workbench(PluginApplicationContext applicationContext) : base(applicationContext)
		{
		}

		#endregion

		#region 公共属性

		public TerminalCommandExecutor Executor
		{
			get
			{
				return _executor ?? CommandExecutor.Default as TerminalCommandExecutor;
			}
			set
			{
				_executor = value;
			}
		}

		#endregion

		#region 打开方法

		protected override void OnStart(string[] args)
		{
			var executor = this.Executor;

			if(executor == null)
			{
				throw new InvalidOperationException("The command executor is null.");
			}

			//调用基类同名方法
			base.OnStart(args);

			//激发“Opened”事件
			this.OnOpened(EventArgs.Empty);

			//启动命令运行器
			executor.Run();

			//关闭命令执行器
			this.Close();
		}

		#endregion
	}
}